package org.jeecg.modules.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.modules.entity.DiyProjectProfessor;
import org.jeecg.modules.entity.ProjectProfessorInfo;
import org.jeecg.modules.service.IDiyProjectProfessorService;
import org.jeecg.modules.service.IProjectProfessorInfoService;
import org.jeecg.modules.util.DingMessageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: lezi
 * @Date: 2022/07/05/12:38
 * @Description:
 */
@Slf4j
@Configuration
@EnableScheduling
public class DynamicScheduleTask {
    private static RestTemplate restTemplate;
    @Autowired
    public  void setRestTemplate(RestTemplate restTemplate) {
        DynamicScheduleTask.restTemplate = restTemplate;
    }
    @Autowired
    private IProjectProfessorInfoService projectProfessorInfoService;
    @Autowired
    private IDiyProjectProfessorService diyProjectProfessorService;
//    0 */10 * * * ?
    @Scheduled(cron = "0 */10 * * * ?")
    private void configureTasks() {

        log.info("20:44");
        HttpServletRequest request=null;
        Map<String, Object> querymap = new HashMap<>();
        log.info("开始处理超时等待的专家"+Calendar.getInstance().getTime());
        querymap.put("project_status","RUNNING");
        List<ProjectProfessorInfo> projectProfessorInfos = projectProfessorInfoService.getBaseMapper().selectByMap(querymap);
        for (ProjectProfessorInfo projectProfessorInfo:projectProfessorInfos) {
            System.out.println(projectProfessorInfo);
            String projectId=projectProfessorInfo.getId();
            List<DiyProjectProfessor> perList = diyProjectProfessorService.getBaseMapper().selectList(new QueryWrapper<DiyProjectProfessor>().eq("project_id", projectProfessorInfo.getId()).eq("professor_in_project_states",2));
//            得到了状态为待审核的专家list
            for (DiyProjectProfessor professor:perList){
                String dingUserId=professor.getDingUserId();
               long nowdate = Calendar.getInstance().getTime().getTime();
               long createdate=professor.getCreateTime().getTime();
               long num=nowdate-createdate;
//                将专家们的时间进行逐一对比
                System.out.println(num/1000/60);
                if(num/1000/60==10){
                    //如果专家一天没有处理则给专家发送消息提醒专家即使处理
                    //给专家推送消息
                    log.info("超时十分钟");
                    String message="请及时处理专家抽取的审批，10分钟后您将被移除参见此项目的资格";
                    SimpleDateFormat formatter= new SimpleDateFormat("HH:mm:ss");
                    Date date = new Date(System.currentTimeMillis());
                    DingMessageUtil.sendMessageToOriginator(dingUserId,"通知时间："+formatter.format(date)+"\n"+"通知内容："+message);
                }
                else if(num/1000/60>=20){
//                    将专家删除
                    log.info("超时二十分钟");
                    String deleteCause="超时处理";
                    String url = "http://localhost:8009" + "/jeecg-boot/deleteOneProfessor/" + projectId + "/" + dingUserId+"/"+ deleteCause+"/";
                    restTemplate.getForObject(url,String.class);
                }
            }
            System.out.println(perList);
        }

        System.out.println(projectProfessorInfos.size());


    }

}
